/********************************
 *CHUCK DINGO
 *By Dacre Denny
 *www.dacredenny.com
 *
 ********************************/

#include <windows.h>

/********************************
 *Engine Includes
 ********************************/
#include "../ExEngine/engine/memoryalloc.h"
#include "../ExEngine/engine/time.h"
#include "../ExEngine/input/dinput.h"
#include "../ExEngine/physics/physics.h"
#include "../ExEngine/engine/engine.h"
#include "../ExEngine/engine/error.h"
#include "../ExEngine/engine/log.h"
#include "../ExEngine/render/d3d.h"
#include "../ExEngine/sound/dsound.h"
#include "../ExEngine/sound/dsound_interfaces.h"


/********************************
 *Game includes
 ********************************/ 
#include "game/core.h"

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
	hPrevInstance = NULL;
	lpCmdLine = NULL;
	nShowCmd = 0;

	try
	{
		if(!CLog::Open())
			throw EE_EXCEPTION("Could not Create log file\n"); 
		
		CEngine::GetInstance()->SetState(true, CEngine::REALTIME); 
		CEngine::GetInstance()->SetContentDirectory(string("ChuckDingo")); 

		if(FAILED(CEngine::GetInstance()->GetWindow()->Create(hInstance, string("Chuck - dacredenny.com"))))
			throw EE_EXCEPTION("Could not Create window\n"); 

		//SetState engine features that are required
		DeviceSettings settings = { false, 800, 600, 32, 32 }; 
		if(FAILED(CEngine::GetInstance()->GetRenderer()->Initailize(settings)))
			throw EE_EXCEPTION("Could not initalize render device\n"); 
		
		if(FAILED(CEngine::GetInstance()->GetInput()->Initailize()))
			throw EE_EXCEPTION("Could not initalize input device\n"); 
			
		if(FAILED(CEngine::GetInstance()->GetSound()->Initailize()))
			throw EE_EXCEPTION("Could not initalize sound device\n"); 
		
		if(FAILED(CEngine::GetInstance()->GetPhysics()->Initailize()))
			throw EE_EXCEPTION("Could not initalize physics engine\n"); 
		
		if(FAILED(CEngine::GetInstance()->GetTime()->Initailize()))
			throw EE_EXCEPTION("Could not initalize timer device\n"); 
		
		CEngine::GetInstance()->SetAppInitializeFunc(&Game::Initalize); 
		CEngine::GetInstance()->SetAppThinkFunc(&Game::Update); 
		CEngine::GetInstance()->SetAppShutdownFunc(&Game::Shutdown);

		CEngine::GetInstance()->StartEngineLoop(); 

		if(FAILED(CEngine::GetInstance()->Release()))
			throw EE_EXCEPTION("Could not Release engine\n"); 
		
		CEngine::GetInstance()->GetWindow()->Release(); 
		
		CEngine::DeleteInstance(); 
		CLog::Close(); 

		return 0; 
	}
	catch(CError::CErrorData error)
	{
		CError::CError(error, ERRORREPORT_LOG);
		CLog::Close(); 
		return -1; 
	}
}